<?php

class ParserController extends CController {

    private $domen;

    public function actionSitemap() {
        //echo $_GET['d'];
        $this->domen = $_GET['d'];
        if (!empty($this->domen)) {
            //echo 0;
            //получаю сырой url
            $rawUrls = $this->getRawUrl(3);
            if (!empty($rawUrls)) {
                //echo 1;
                foreach ($rawUrls as $rawUrl) {
                    
                    if (!empty($rawUrl)) {
                        //echo 2;
                        //получаю страницу
                        $html = Parser::getHtmlPage($rawUrl->url);

                        if (!empty($html)) {

                            //echo 3;
                            $this->parsingEmail($html, $rawUrl);
                            
                            $html = str_get_html($html);
                            echo $rawUrl->url;
                            foreach ($html->find('a') as $element) {
                                //echo "5".$element->href;
                                if (!preg_match("/mailto\:/i", $element->href)) {
                                    if ((preg_match("/^http:\/\//i", $element->href)) || (preg_match("/^https:\/\//i", $element->href))) {
                                        //echo 6;
                                        if (preg_match("/^http:\/\/(.*)?{$rawUrl->domen}/i", $element->href)) {
                                            if($this->chekUrl($element->href)){
                                                $model = new Sitemap();
                                                $model->domen = $this->domen;
                                                $model->url = $element->href;
                                                $model->parsing_date = 0;
                                                $model->status = 0;
                                                $model->save();
                                            }
                                        }
                                    } else {
                                        //echo 7;
                                        if ($element->href != '/') {
                                            //echo 8;
                                            if($this->chekUrl($element->href)){
                                                $model = new Sitemap();
                                                $model->domen = $this->domen;
                                                $model->url = "http://" . $this->domen . $element->href;
                                                $model->parsing_date = 0;
                                                $model->status = 0;
                                                $model->save();
                                            }
                                        }
                                    }
                                }
                            }

                            $rawUrl->parsing_date = time();
                            $model->status = 0;
                            $rawUrl->update();
                        }
                    }
                }
            } else {
                echo "Не необработаных страниц";
            }
        }
        //echo 9;
    }

    private function parsingEmail($html, $rawUrl) {
        preg_match_all('/([A-Za-z0-9_\-]+\.)*[A-Za-z0-9_\-]+@([A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]\.)+[A-Za-z]{2,4}/u', $html, $pEmail);
        $emails = array_unique($pEmail[0]);
        //print_r($emails);
        foreach ($emails as $key => $email) {
            preg_match_all("/{$this->domen}/", $email, $t);
            if (empty($t[0])) {
                $model = new Email();
                $model->sitemap_id = $rawUrl->id;
                $model->email = $email;
                $model->status = 0;
                $model->save();
                //print_r($model->getErrors());
            }
        }
    }

    // получить сырые url
    private function getRawUrl($limit = 5) {
        $criteria = new CDbCriteria;
        $criteria->condition = 'domen = :domen AND parsing_date < :parsing_date';
        $criteria->params = array(
            ':domen' => $this->domen,
            ':parsing_date' => time() - (7 * 24 * 60 * 60),
        );
        $criteria->limit = $limit;
        $criteria->offset = '0';

        $models = Sitemap::model()->findAll($criteria);

        return $models;
    }
    
    // url фильтр
    private function chekUrl($url) {
        $filters = array('.jpg', '.jpeg', '.gif', '.png', 'vkontakte', 'facebook', 'twitter');
        
        $chek = true;
        
        foreach ($filters as $filter){
            $pos = strpos($url, $filter);
            if ($pos === false) {
                $chek = false;
            }
        }
        
        return $chek;
    }

}